local M = _G.ServerOHOH.class.new(script,_G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ServerOHOH = _G.ServerOHOH;
local ItemTool=_G.CommonOHOH.load.ItemTool()
local Task_Cfg = _G.CommonOHOH.config.Task
local EventList = ServerOHOH.load.EventList();
local function_list = ServerOHOH.function_list;
function M.new(self,...)
    M(self,...);
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.player = self.parent
    self.DataStore = self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent());
    self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl());
    self.taskObjList = {}
    self:InitTask()
end

function M.InitTask(self)
    local taskList = self.DataStore:GetData("task")
    local regularTaskList = self.DataStore:GetData("regularTask")
    for id, value in pairs(Task_Cfg) do
        if id<=100 then
            --task数据中是否有基础任务
            local taskName = "Regular_"..value.className.."_"..id
            if not taskList[taskName] then
                --task无数据，初始化基础任务
                local taskData = "task_Regular_"..value.className.."_"..id
                local regularData = "regularTask_Regular_"..value.className.."_"..id
                self.ItemCtrl:AddItem({[taskData]=0},true,true)
                self.ItemCtrl:AddItem({[regularData]={
                    taskId = id,
                    isClaim = false
                }},true,true)
            end
            --taskId配置表里的id：2,3
            local taskId = regularTaskList[taskName].taskId
            local className = Task_Cfg[taskId].className
            local taskClass = _G.CommonOHOH.load[className]()
            self.taskObjList[taskName]=taskClass.new(self.parent,Task_Cfg[taskId].goalValue,taskName)
        end
    end
    self:Fire(EventList.init_task);
end
--领取奖励
function M.ClaimAward(self,taskName)
    if not self.taskObjList[taskName]:IsComplete() then
        return
    end
    --发放奖励
    local regularTaskList = self.DataStore:GetData("regularTask")
    local taskId = regularTaskList[taskName].taskId
    local taskCfg = Task_Cfg[taskId]
    local list = nil
    local value = nil
    for index, v in pairs(taskCfg.AwardValue) do
        list = string.split(index,"_")
        value =v
    end
    if not self.RngDataCtrl then
        self.RngDataCtrl = self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    end
    if list[#list]=="Lucky" then
        self.RngDataCtrl:AddLucky(value,true)
    elseif list[#list]=="ShinyLuck" then  
        self.RngDataCtrl:AddShinyLucky(value,true)
    elseif list[#list]=="RainbowLuck" then  
        self.RngDataCtrl:AddRainbowLucky(value,true)
    elseif list[#list]=="VoidLuck" then
        self.RngDataCtrl:AddVoidLucky(value,true)
    elseif list[#list]=="RollSpeed" then
        self.RngDataCtrl:AddRollSpeed(value,true)
    end
    if taskCfg.nextId ==0 then
        self.DataStore:GetData("regularTask").__real[taskName].isClaim = true
        self.DataStore:Execut(function()
        end);
        self.parent:SynchDataToClient("regularTask");
        return false
    else
        self.DataStore:GetData("regularTask").__real[taskName].taskId =  taskCfg.nextId
        self.DataStore:GetData("task").__real[taskName] = 0
        local className = taskCfg.className
        local taskClass = _G.CommonOHOH.load[className]()
        self.taskObjList[taskName]=taskClass.new(self.parent,Task_Cfg[taskCfg.nextId].goalValue,taskName)
        self.DataStore:Execut(function()
        end);
        self.parent:SynchDataToClient("task");
        self.parent:SynchDataToClient("regularTask");
        return true
    end
end


--传入taskName
M:RegistFunction(function_list.claim_award,M.ClaimAward)


function M.dtor(self)
    M.super.dtor(self)
end

return M